package com.shunlin.paopaoku.box2d;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.shunlin.paopaoku.game.*;

//碰撞检测类

public class Box2DContactListener implements ContactListener {
    public static boolean isOnGround = false; //判断物体是否在地上

    //开始碰撞当发生两个物体发生接触点的时候调用
    @Override
    public void beginContact(Contact p1) {
        //当夹具A拿到的数据不等于空
        if (p1.getFixtureA().getUserData() != null) {
            //当夹具A拿到的数据等于foot
            if (p1.getFixtureA().getUserData().equals("foot")) {
                isOnGround = true;
            }
        }

        if (p1.getFixtureB().getUserData() != null) {
            if (p1.getFixtureB().getUserData().equals("foot")) {
                isOnGround = true;
            }
        }

        //当夹具A拿到数据不等于空，并且夹具B拿到数据不等于空
        if (p1.getFixtureA().getUserData() != null && p1.getFixtureB().getUserData() != null) {
            //当夹具A拿到的数据等于player,并且夹具B拿到的数据不等于foot不等于ground 这样才能拿到下标进行判断
            if ((p1.getFixtureA().getUserData().equals("player") && (!p1.getFixtureB().getUserData().equals("foot") && !p1.getFixtureB().equals("ground")))) {
                System.out.println("beginContact");
                Gdx.app.log("Index", ((int) p1.getFixtureB().getUserData()) + "");
                ClassManager.b2dWorld.killStar((int) p1.getFixtureB().getUserData());
            } else if ((!p1.getFixtureA().getUserData().equals("foot") && !p1.getFixtureA().equals("ground")) && p1.getFixtureB().equals("player")) {
                System.out.println("beginContact");
                Gdx.app.log("Index", ((int) p1.getFixtureA().getUserData()) + "");
                ClassManager.b2dWorld.killStar((int) p1.getFixtureA().getUserData());
            }
        }
    }

    //当两个物体之间的接触点消失时调用
    @Override
    public void endContact(Contact p1) {
        if (p1.getFixtureA().getUserData() != null) {
            if (p1.getFixtureA().getUserData().equals("foot")) {
                isOnGround = false;
            }
        }

        if (p1.getFixtureB().getUserData() != null) {
            if (p1.getFixtureB().getUserData().equals("foot")) {
                isOnGround = false;
            }
        }

        if (p1.getFixtureA().getUserData() != null && p1.getFixtureB().getUserData() != null) {
            if ((p1.getFixtureA().getUserData().equals("player") && (!p1.getFixtureB().getUserData().equals("foot") && !p1.getFixtureB().equals("ground")))) {
                System.out.println("endContact");
            } else if ((!p1.getFixtureA().getUserData().equals("foot") && !p1.getFixtureA().equals("ground")) && p1.getFixtureB().equals("player")) {
                System.out.println("endContact");
            }
        }
    }

    @Override
    public void preSolve(Contact p1, Manifold p2) {
    }

    @Override
    public void postSolve(Contact p1, ContactImpulse p2) {
    }

}
